<template>
  <div>
    <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="1" @click='reloadList()'>出售中的商品</el-menu-item>
      <el-menu-item index="2">仓库中的商品</el-menu-item>
      <el-menu-item index="3">已经售罄的商品</el-menu-item>
      <el-menu-item index="4">商品回收站</el-menu-item>
    </el-menu>
    <div class="block">
      <span class="demonstration">商品分类:</span>
      <el-cascader placeholder="试试搜索：手机" :options="options" :props="{emitPath:false}" v-model='searchEntity.catalogId'
        filterable></el-cascader>
      &emsp;&emsp;&emsp;
      <span class="demonstration">商品搜索:</span>
      <el-input placeholder="请输入内容" style="width:250px" v-model='searchEntity.seek'></el-input>
      <el-button type="primary" round @click="reloadList(num)">查询</el-button>
    </div>
    <el-button type="primary" @click="toAddGoods()">添加商品</el-button>
    <template>
      <el-table :data="goodsList" stripe style="width: 100%">
        <el-table-column prop="id" label="id">
        </el-table-column>
        <el-table-column prop="spuImage" label="商品图片">
          <template slot-scope="scope">
            <img :src="scope.row.spuImage" alt="未加载到图片" width="100px" height="100px">
          </template>
        </el-table-column>
        <el-table-column prop="spuName" label="商品名称">
        </el-table-column>
        <el-table-column prop="spuPrice" label="商品售价">
        </el-table-column>
        <el-table-column prop="salesVolume" label="销量">
        </el-table-column>
        <el-table-column prop="repertory" label="库存">
        </el-table-column>
        <el-table-column prop="orderColumn" label="排序">
        </el-table-column>
        <el-table-column prop="publishStatus" label="状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.publishStatus" active-color="#13ce66" inactive-color="#ff4949"
              active-text="上架" inactive-text="下架" :active-value="1" :inactive-value="0"
              @change="changeStatus($event,scope.row,scope.$index)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="操作时间">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row.id)" type="text" size="small">编辑</el-button>
            <el-button type="text" v-if="num!=4" size="small" @click="addRecycle(scope.row.id)">加入回收站</el-button>
            <el-button type="text" v-if="num==4" size="small" @click="recoverRecycleGoods(scope.row.id)">恢复商品
            </el-button>
            <el-button type="text" v-if="num==4" size="small" @click="deleteRecycleGoods(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <div class="block">
      <el-pagination @size-change="reloadList(num)" @current-change="reloadList(num)"
        :current-page.sync="searchEntity.page" :page-sizes="[10 , 20, 30, 40]" :page-size.sync="searchEntity.limit"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具 js，第三方插件 js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import 引入的组件需要注入到对象中才能使用
  components: {},
  props: {},
  data () {
    //这里存放数据
    return {
      token: "",
      num: 1,
      activeIndex: '1',
      activeIndex2: '1',
      total: 0,
      goodsList: [],
      options: [],
      searchEntity: {
        limit: 10,
        page: 1
      },
    };
  },
  //计算属性 类似于 data 概念
  computed: {
    headers () {
      return {
        "token": (localStorage.token || ""),
        "type": "1"
      }
    }
  },
  //监控 data 中的数据变化
  watch: {},
  //方法集合
  methods: {
    handleClick (id) {//编辑商品
      this.$router.push({
        path: '/addgoods',
        query: {
          id: id
        }
      })
    },
    toAddGoods () {//添加商品
      this.$router.replace("/addgoods");
    },
    handleSelect (key, keyPath) {
      this.num = key;
      console.log(key);
      this.reloadList(key);
    },
    deleteRecycleGoods (id) {//删除回收站商品
      this.$http
        .get('http://localhost:8001/coco/goods/goods/deleteRecycleGoods/' + id, {
          headers: {
            "token": this.token,
            "type": "1"
          }
        })
        .then(({ data }) => {
          if (data.code == 0) {
            this.$message({
              message: '删除商品成功!',
              type: 'success'
            });
            this.reloadList(this.num);
          } else {
            this.$message.error('删除商品失败!');
          }
        })
        .catch((err) => { });
    },
    recoverRecycleGoods (id) {//恢复商品
      this.$http
        .get('http://localhost:8001/coco/goods/goods/recoverRecycleGoods/' + id, {
          headers: {
            "token": this.token,
            "type": "1"
          }
        })
        .then(({ data }) => {
          if (data.code == 0) {
            this.$message({
              message: '恢复商品成功!',
              type: 'success'
            });
            this.reloadList(this.num);
          } else {
            this.$message.error('恢复商品失败!');
          }
        })
        .catch((err) => { });
    },
    addRecycle (id) {//加入回收站
      this.$http
        .get('http://localhost:8001/coco/goods/goods/addRecycle/' + id, {
          headers: {
            "token": this.token,
            "type": "1"
          }
        })
        .then(({ data }) => {
          if (data.code == 0) {
            this.$message({
              message: '加入回收站成功!',
              type: 'success'
            });
            this.reloadList(this.num);
          } else {
            this.$message.error('加入回收站失败!');
          }
        })
        .catch((err) => { });
    },
    changeStatus (data, row, index) {//上下架操作
      console.log(data, row, index);
      this.$http
        .get("http://localhost:8001/coco/goods/goods/putAway/" + row.id + "/" + data, {
          headers: {
            "token": this.token,
            "type": "1"
          }
        })
        .then(({ data }) => {
          console.log(data);
          if (data.data) {
            this.$message({
              message: '修改成功!',
              type: 'success'
            });
            this.reloadList(this.num);
          } else {
            let newData = b;
            newData.publishStatus = newData.publishStatus == 1 ? "0" : "1";
            this.goodsList[index] = newData;
          }
        })
        .catch((err) => { });
    },
    reloadList (num) {
      if (num == 1) {
        this.saleList();
      }
      if (num == 2) {
        this.warehouseList();
      }
      if (num == 3) {
        this.findSelloutGoods();
      }
      if (num == 4) {
        this.findRecycleGoods();
      }
    },
    warehouseList () {//仓库中的商品
      this.$http
        .post("http://localhost:8001/coco/goods/goods/warehouseList", this.searchEntity, {
          headers: {
            "token": this.token,
            "type": "1"
          }
        })
        .then(({ data }) => {
          console.log(data.data.rows);
          this.goodsList = data.data.rows;
          this.total = data.data.total;
        })
        .catch((err) => { });
    },
    findSelloutGoods () {//售罄的商品
      this.$http
        .post("http://localhost:8001/coco/goods/goods/findSelloutGoods", this.searchEntity, {
          headers: {
            "token": this.token,
            "type": "1"
          }
        })
        .then(({ data }) => {
          this.goodsList = data.data.rows;
          this.total = data.data.total;
        })
        .catch((err) => { });
    },
    saleList () {//出售中商品
      this.$http
        .post("http://localhost:8001/coco/goods/goods/saleList", this.searchEntity, {
          headers: {
            "token": this.token,
            "type": "1"
          }
        })
        .then(({ data }) => {
          if (data.code != 0) {
            this.$message({
              message: data.message,
              type: 'error'
            })
            this.$router.push("/");
          }
          this.goodsList = data.data.rows;
          this.total = data.data.total;
        })
        .catch((err) => { });
    },
    findRecycleGoods () {//商品回收站列表
      this.$http
        .post("http://localhost:8001/coco/goods/goods/findRecycleGoods", this.searchEntity, {
          headers: {
            "token": this.token,
            "type": "1"
          }
        })
        .then(({ data }) => {
          this.goodsList = data.data.rows;
          this.total = data.data.total;
        })
        .catch((err) => { });
    },
    getCategoryList () {//商品分类查询
      this.$http
        .get("http://localhost:8001/coco/goods/category/findTree", {
          headers: {
            "token": this.token,
            "type": "1"
          }
        })
        .then(({ data }) => {
          this.options = data.data;
          console.log(this.options);
        })
        .catch((err) => { });
    }
  },
  //生命周期 - 创建完成（可以访问当前 this 实例）
  created () {
    this.token = localStorage.getItem("token");
    this.reloadList(1);
    this.getCategoryList();
  },
  //生命周期 - 挂载完成（可以访问 DOM 元素）
  mounted () {

  },
  beforeCreate () { }, //生命周期 - 创建之前
  beforeMount () { }, //生命周期 - 挂载之前
  beforeUpdate () { }, //生命周期 - 更新之前
  updated () { }, //生命周期 - 更新之后
  beforeDestroy () { }, //生命周期 - 销毁之前
  destroyed () { }, //生命周期 - 销毁完成
  activated () { }, //如果页面有 keep-alive 缓存功能，这个函数会触发
}
</script>
<style scoped>
</style>